const Koa = require('koa');
const compress = require('koa-compress')
const screenshot = require('./screenshot')
const app = new Koa()

app.use(compress({
  filter: function (content_type) {
    return /text/i.test(content_type)
  },
  flush: require('zlib').Z_SYNC_FLUSH
}))

app.use(async ctx => {
    var url = ctx.query.url
    console.log('goto:', url)
    if (!/^https?:\/\/.+/.test(url)) {
      ctx.body = 'url 不合法'
    } else {
      if (!isNaN(ctx.query.wait)) {
        ctx.query.wait = ~~ctx.query.wait
      }
      let data = await screenshot(url, ctx.query.wait, ctx.query.width ? ~~ctx.query.width : undefined)
      if (ctx.query.base64) {
        ctx.body = 'data:image/jpeg;base64,' + data
      } else {
        ctx.body = `<img src="data:image/png;base64,${data}" />`
      }
    }
});

app.listen(8000)
console.log('server start success at 8000')
//process.on('unCaughtException', function (err) {
//  console.log(err)
//})
